package com.yhb.controller;

import com.yhb.conf.ResultCode;
import com.yhb.controller.form.SimpleVerifyCodeForm;
import com.yhb.controller.form.UnionPayScanForm;
import com.yhb.controller.vo.SimpleVerifyCodeTokenVo;
import com.yhb.controller.vo.SingleValueWrapVo;
import com.yhb.controller.vo.UnionPayScanVo;
import com.yhb.model.Card;
import com.yhb.model.RapidCheat;
import com.yhb.model.User;
import com.yhb.yinsheng.UnionPayService;
import com.yhb.yinsheng.model.OrderQueryResp;
import com.yhb.yinsheng.model.QRCode;
import lombok.var;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.EncryptedRequestBody;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.annotations.SignedRequestBody;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/union")
public class UnionPayController extends BaseController {


    @Autowired
    private UnionPayService unionPayService;

    @PostMapping("/checkBound")
    @NeedAuth
    public Response<SingleValueWrapVo<Boolean>> checkCardBounded(@SignedRequestBody Card card, @CurrentUser User user) {
        return Response.ok(new SingleValueWrapVo<>(unionPayService.checkCardBounded(card.getId(), user.getId())));
    }

    @PostMapping("/getBindCardSms")
    @NeedAuth
    public Response<SimpleVerifyCodeTokenVo> getBindCardSms(@SignedRequestBody Card card, @CurrentUser User user) {
        return Response.ok(unionPayService.bindCard(card.getId(), user));
    }

    @PostMapping("/verifyBindCard")
    @NeedAuth
    public Response verifyBindCard(@SignedRequestBody SimpleVerifyCodeForm form, @CurrentUser User user) {
        unionPayService.verifyBindCard(form.getToken(), form.getVerifyCode(), user);
        return Response.ok();
    }

    @PostMapping("/genQR")
    @NeedAuth
    public Response<QRCode> genQR(@SignedRequestBody Card card, @CurrentUser User user) {
        throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "通道维护中");
//        var qr = unionPayService.genQRCode(user, card.getId());
//        return Response.ok(qr);
    }

    @PostMapping("/scan")
    @NeedAuth
    public Response<UnionPayScanVo> reverseScan(@CurrentUser User user, @EncryptedRequestBody UnionPayScanForm form) {
        var orderId = unionPayService.reverseScanQR(form.getProvince(), form.getCity(), form.getQrCode(), form.getAmount(), user, form.getRemitCardId());
        return Response.ok(UnionPayScanVo.builder().orderId(orderId).build());
    }

    @PostMapping("/query")
    @NeedAuth
    public Response<RapidCheat> queryOrderStatus(@SignedRequestBody UnionPayScanVo order, @CurrentUser User user) {
        return Response.ok(unionPayService.queryOrder(order.getOrderId(), user.getId()));
    }

    @GetMapping("/queryBalance")
    @NeedAuth
    public Response<SingleValueWrapVo<BigDecimal>> queryOrderStatus(@CurrentUser User user) {
        return Response.ok(new SingleValueWrapVo<>(unionPayService.queryBalance(user)));
    }
}

